/**
 * 
 */
package com.music.profile.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.music.core.datastoreaccess.service.DataStoreAccessorService;


/**
 * @author spradeep
 *
 */
public class DataStoreController extends AbstractController {
	private DataStoreAccessorService dataAccessorService;
	private boolean enabled;
	
	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public DataStoreAccessorService getDataStoreAccessorService() {
		return dataAccessorService;
	}

	public void setDataStoreAccessorService(DataStoreAccessorService memberService) {
		this.dataAccessorService = memberService;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		System.out.println("DataStore Access: Enabled="+enabled);
		if(!enabled) {
			res.getWriter().append(enabled+"");
			return null;
		}
		Map parameterMap = req.getParameterMap();
		Set keySet = parameterMap.keySet();
		for (Object object : keySet) {
			String key = (String)object;
			System.out.println(key+"="+req.getParameter(key));
		}
		
		
		String kind = req.getParameter("kind");
		if(kind!=null && !kind.equals("")) {
		Class clazz = Class.forName("com.music.core."+kind.toLowerCase()+"s.beans."+kind);
		Map<String,Object> model = new HashMap<String, Object>();
		model.put("data", dataAccessorService.getallObjects(clazz));
		System.out.println(dataAccessorService.getallObjects(clazz));
		return new ModelAndView("dataResults",model);
		} 
		String query = req.getParameter("query");
		System.out.println(query);
		List<Object> result = dataAccessorService.query(query);
		Map<String,Object> model = new HashMap<String, Object>();
		model.put("data", result);
		System.out.println(result);
		return new ModelAndView("dataResults",model);
	}
 
}
